#include"UASSprite.h"


const DWORD VERTEX_T::fvf = D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1;

UASSprite::UASSprite()
{
	
}

UASSprite::UASSprite(const IDirect3DDevice9 *device,const string texName)
{
	//	create texture
	LPCWSTR test = StringHelper::ParaseToLPCWSTR(texName);
	if(FAILED(D3DXCreateTextureFromFile((IDirect3DDevice9*)device,test,&_pTex)))
	{
		MessageBox(NULL,TEXT("UASSprite load texture failed."),TEXT("Error"),MB_OK);
	}
}

UASSprite::~UASSprite()
{
	if(_pTex!=NULL)
	{
		_pTex->Release();
		_pTex = NULL;
	}
}


void UASSprite::LoadAuroraSprite(char* filename)
{
	assert(_pTex);
	AuroraSprite::LoadAuroraSprite(filename);
}


void UASSprite::Render_2D(IDirect3DDevice9* const device,
							int x,int y,int w,int h,int u,int v,int ul,int vl,
							D3DXCOLOR c0,D3DXCOLOR c1,D3DXCOLOR c2,D3DXCOLOR c3,
							bool bTrans,float alpha)
{
	assert(device);
	assert(_pTex);


	VERTEX_T vertice[4];
	D3DXMATRIX proj,identity;
	D3DXMatrixIdentity(&identity);
	D3DXMatrixOrthoOffCenterLH(&proj,0.0f,(float)1024,-(float)768,0.0f,-100.0f,100.0f);			//	1024 x 768 is temp hard code

	device->SetTransform(D3DTS_WORLD,&identity);
	device->SetTransform(D3DTS_VIEW,&identity);
	device->SetTransform(D3DTS_PROJECTION,&proj);

	device->SetRenderState(D3DRS_ZENABLE,FALSE);
	device->SetRenderState(D3DRS_LIGHTING,FALSE);			//	temp disable lighting ,cause VERTEX_T has no material now.

	if(bTrans)
	{	
		//	If we want to change alpha althoris,we should modify here code.
		device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
		device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
		device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);

		c0.a = alpha;
		c1.a = alpha;
		c2.a = alpha;
		c3.a = alpha;
	}

	vertice[0] = VERTEX_T(     x ,         -y ,0,c0 , float(u)/float(_width),	 float(v)/float(_height));		//	up_left
	vertice[1] = VERTEX_T( x + w ,         -y ,0,c1 , float(u+ul)/float(_width), float(v)/float(_height));		//	up_right
	vertice[2] = VERTEX_T(     x ,  -( y + h) ,0,c2 , float(u)/float(_width),	 float(v+vl)/float(_height));		//	down_left
	vertice[3] = VERTEX_T( x + w ,  -( y + h) ,0,c3 , float(u+ul)/float(_width), float(v+vl)/float(_height));		//	down_right

	//device->SetFVF(VERTEX_T::fvf);
	device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);
	device->SetTexture(0,_pTex);
	device->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP,2,vertice,sizeof(VERTEX_T));	

	device->SetRenderState(D3DRS_ZENABLE,true);
	device->SetRenderState(D3DRS_ALPHABLENDENABLE,false);
	device->SetRenderState(D3DRS_LIGHTING,true);
}

void UASSprite::PaintModule(IDirect3DDevice9* const device,int curModuleIdx,int x,int y,int ox = 0,int oy = 0)
{
	assert(_pTex);

	SpriteModule curModule = _allModules[curModuleIdx];
	Render_2D(device,x + ox,y + oy,
				curModule._width,curModule._height,curModule._u,curModule._v,curModule._width,curModule._height,
				D3DCOLOR_ARGB(255,255,255,255),D3DCOLOR_ARGB(255,255,255,255),
				D3DCOLOR_ARGB(255,255,255,255),D3DCOLOR_ARGB(255,255,255,255),
				true,1.0f);
}


void UASSprite::PaintFModule(IDirect3DDevice9* const device,string curModuleId,int x,int y,int ox = 0,int oy = 0)
{
	int iModulesCount = _allModules.size();
	for(int idx = 0;idx <  iModulesCount; idx++)
	{
		if(_allModules[idx]._moduleId == curModuleId)
		{
			PaintModule(device,idx,x,y,ox,oy);
			return;
			//break;
		}
	}
}

void UASSprite::PaintFrame(IDirect3DDevice9* const device,int curFrameIdx,int x,int y)
{
	SpriteFrame curFrame = _allFrames[curFrameIdx];

	for(vector<SpriteFModule>::iterator it = curFrame._allFModules.begin(); it != curFrame._allFModules.end(); it++)
	{
		PaintFModule(device,(*it)._moduleId,x,y,(*it)._ox,(*it)._oy);
	}
}

void UASSprite::PaintAFrame(IDirect3DDevice9* const device,string frameId,int x,int y,int ox = 0,int oy = 0)
{
	size_t frameCount = _allFrames.size();
	for(size_t idx = 0;idx < frameCount;++idx)
	{
		if(_allFrames[idx]._frameId == frameId)
		{
			PaintFrame(device,idx,x + ox, y + oy);
			return;
		}
	}
}